<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Air Conditioner Add</title>

    <link href="https://cdn.staticfile.org/twitter-bootstrap/3.4.1/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}" rel="stylesheet">

    <link href="../../static/layui/css/layui.css" th:href="@{/webjars/layui/css/layui.css}" rel="stylesheet">

    <script type="text/javascript" src="https://cdn.staticfile.org/jquery/3.4.0/jquery.min.js" th:src="@{/webjars/jquery/jquery.min.js}"></script>
    <script type="text/javascript" src="https://cdn.staticfile.org/twitter-bootstrap/3.4.1/js/bootstrap.min.js" th:src="@{/webjars/bootstrap/js/bootstrap.min.js}"></script>
    <script type="text/javascript" src="https://cdn.staticfile.org/distpicker/2.0.5/distpicker.min.js"></script>
    <script type="text/javascript" src="https://cdn.staticfile.org/store.js/1.3.20/store.min.js" th:src="@{/webjars/store.js/store.min.js}"></script>

    <script type="text/javascript" src="../../static/layui/layui.all.js" th:src="@{/webjars/layui/layui.all.js}"></script>
    <script type="text/javascript" src="../../static/layui/layui.js" th:src="@{/webjars/layui/layui.js}"></script>
    <script type="text/javascript" src="../../static/js/common.js" th:src="@{/js/common.js}"></script>
    <script type="text/javascript" src="../../static/js/air.conditioner.js" th:src="@{/js/air.conditioner.js}"></script>
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">

    <!-- 头部导航栏 -->
    <div id="header-navigation-div-id"></div>

    <!-- 侧边导航栏 -->
    <div id="side-navigation-div-id"></div>

    <div class="layui-body" style="left: 300px;">
        <!-- 内容主体区域 -->
        <div style="padding: 15px;">
            <div class="layui-container" style="
                border: 1px solid #1E9FFF;
                float: right;
                margin: 50px 200px 0 0;
                padding: 20px;
                background-color: #e2e2e2;
                border-radius: 10px;
                  ">
                <h3 style="text-align: center">设备信息填写</h3>
                <form id="add-info-form" action="#" method="post" class="form-horizontal" role="form" style="width: 95%; margin-top: 50px;">
                    <div class="form-group">
                        <label for="brand" class="col-sm-2 control-label">品牌</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="brand" name="brand" placeholder="请输入品牌">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="model" class="col-sm-2 control-label">型号规格</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="model" name="model" placeholder="请输入型号规格">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="seller" class="col-sm-2 control-label">销售单位</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="seller" name="seller" placeholder="请输入销售单位">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="address" class="col-sm-2 control-label"><span style="margin-right: 10px;font-size: 17px;color: red;">*</span>设备地址</label>
                        <div class="col-sm-10">
                            <div data-toggle="distpicker" id="address">
                                <select class="form-control" id="address-province" name="address.province" data-province="---- 选择省 ----" style="width: 32%; display: inline-block"></select>
                                <select class="form-control" id="address-city" name="address.city" data-city="---- 选择市 ----" style="width: 31%; display: inline-block; margin: 0 2%"></select>
                                <select class="form-control" id="address-district" name="address.district" data-district="---- 选择区 ----" style="width: 32%;  display: inline-block"></select>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="address-detail" class="col-sm-2 control-label">详细地址</label>
                        <div class="col-sm-10">
                            <textarea class="form-control" id="address-detail" name="address.detail" rows="3" style="resize:none" placeholder="请输入详细地址"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10" style="width: 40%; margin: 0 auto">
                            <button class="btn btn-success" id="add-btn" style="margin: 20px 0 0 65%; width: 100px;">添加</button>
                        </div>
                        <div class="col-sm-offset-2 col-sm-10" style="width: 40%; margin: 0 auto">
                            <button class="btn btn-danger" id="reset-btn" style="margin: 20px 0 0 65%; width: 100px;">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- 页脚 -->
    <div id="footer-div-id"></div>
</div>

<script type="text/javascript">

    window.onload = function (ev) {

        // 页面初始化
        pageAfterOnloadInit();

        // 设置“添加”按钮事件
        document.getElementById('add-btn').onclick = addInfoFormSubmitEvent;

        // 设置“重置”按钮事件
        document.getElementById('reset-btn').onclick = addInfoFormResetEvent;

        // 退出事件
        document.getElementById('logout-btn-id').onclick = logoutEvent;

    }

</script>

<script type="text/javascript">

    //////////////// function defined ///////////////////////////////

    // 页面初始化
    function pageAfterOnloadInit() {
        initHeaderNavigationDiv();
        initSideNavigationDivOrdinaryEquipment();
        initFooterDiv();
        // layui 初始化
        layuiInit();
    }

    // 添加信息表单按钮事件
    function addInfoFormSubmitEvent(ev) {

        // 表单校验
        if (!checkAddressInfoAndTip()) {
            return false;
        }

        // 发送添加设备信息请求
        $.ajax({
            url: AIR_CONDITIONER_BASE_MAPPING_V1 + "/add",
            type: "post",
            data: {
                'brand': $("#brand").val(),
                'model': $("#model").val(),
                'seller': $("#seller").val(),
                'address.province': $("#address-province").val(),
                'address.city': $("#address-city").val(),
                'address.district': $("#address-district").val(),
                'address.detail': $("#address-detail").val()
            },
            success: function (result, status, xhr) {
                var obj = JSON.parse(result);
                var code = obj.code;
                var message = obj.message;
                var data = obj.data;
                if (code !== 200) {
                    kingLayerMsg(message, airConditionerDefaultLayerMsgOptions);
                } else {
                    kingLayerMsg("添加成功！", airConditionerDefaultLayerMsgOptions);
                    resetAddressInfo();
                }
            }
        });

        return false;
    }

    // “重置”按钮事件
    function addInfoFormResetEvent(ev) {
        document.getElementById("brand").value = "";
        document.getElementById("model").value = "";
        document.getElementById("seller").value = "";
        resetAddressInfo();
        return false;
    }

    // 重置地址选择
    function resetAddressInfo() {
        document.getElementById("address-province").options[0].selected = true;
        document.getElementById("address-city").options[0].selected = true;
        document.getElementById("address-district").options[0].selected = true;
        document.getElementById("address-detail").value = "";
        return false;
    }

</script>
</body>
</html>